package com.hairline.hairlinemarket.service.impl;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.hairlinemarket.bean.MarketAddress;
import com.hairline.hairlinemarket.bean.MarketAddressExample;
import com.hairline.hairlinemarket.bean.MarketUser;
import com.hairline.hairlinemarket.bean.vo.AddressVo;
import com.hairline.hairlinemarket.mapper.MarketAddressMapper;
import com.hairline.hairlinemarket.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author:杨小凯
 * Date:2023-04-22 15:39
 * Description:<描述>
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private MarketAddressMapper marketAddressMapper;

    @Override
    public PageInfo selectAddressByUserId(Integer userId) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        PageHelper.startPage(1, 10);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        return pageInfo;
    }

    @Override
    public Integer addAddress(MarketAddress marketAddress) {

        // 获取信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        // 拿到了个人id信息
        Integer userId = principal.getId();
        // 根据id查询地址
        MarketAddress hasAddress = marketAddressMapper.selectByPrimaryKey(marketAddress.getId());
        if (hasAddress!=null){
            // 直接更新数据
            MarketAddressExample addressExample = new MarketAddressExample();
            addressExample.createCriteria().andIdEqualTo(marketAddress.getId());
            marketAddressMapper.updateByExampleSelective(marketAddress, addressExample);

            // 判断是否设为默认
            if (marketAddress.getIsDefault()){
                // 修改该用户的其他默认值
                MarketAddress record = new MarketAddress();
                record.setIsDefault(false);
                MarketAddressExample example = new MarketAddressExample();
                example.createCriteria().andUserIdEqualTo(userId).andIdNotEqualTo(marketAddress.getId());
                marketAddressMapper.updateByExampleSelective(record, example);

            }
        }else {
            marketAddress.setUserId(userId);
            marketAddress.setId(null);
            marketAddress.setAddTime(new Date());
            marketAddress.setUpdateTime(new Date());
            marketAddressMapper.insertSelective(marketAddress);
            // 获取插入后的id
            MarketAddressExample marketAddressExample = new MarketAddressExample();
            marketAddressExample.createCriteria().andUserIdEqualTo(userId)
                    .andAddressDetailEqualTo(marketAddress.getAddressDetail())
                    .andAreaCodeEqualTo(marketAddress.getAreaCode())
                    .andCityEqualTo(marketAddress.getCity())
                    .andCountyEqualTo(marketAddress.getCounty())
                    .andIsDefaultEqualTo(marketAddress.getIsDefault())
                    .andNameEqualTo(marketAddress.getName())
                    .andProvinceEqualTo(marketAddress.getProvince())
                    .andTelEqualTo(marketAddress.getTel());
            List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
            MarketAddress address = marketAddresses.get(0);
            return address.getId();
        }
        return null;
    }

    @Override
    public MarketAddress selectPrimkey(Integer id) {
        MarketAddress address = marketAddressMapper.selectByPrimaryKey(id);
        return address;
    }

    @Override
    public void deleteByPrimkey(Integer id) {
        // 逻辑删除
        MarketAddress record = new MarketAddress();
        record.setUpdateTime(new Date());
        record.setDeleted(true);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.createCriteria().andIdEqualTo(id);
        marketAddressMapper.updateByExampleSelective(record, marketAddressExample);
    }
}
